<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;
use app\service\ArticleService;

//文章
class Article extends BaseController
{
    protected $user;
    public function initialize()
    {
        $this->user = Db::name('admin')->where(['token'=>cookie('admin_token_login')])->find();
    }
    public function index()
    {
        $p=input();
        $list=[];
        $category_id=0;
        $pager='';
        if(!empty($p['tag']))
        {
            $category=\app\service\CategoryService::infoByTag(['where'=>['sign'=>$p['tag']]]);
            
            $category_id=$category['id'];
            $child_category_id=\app\service\CategoryService::childId(['where'=>[['parent_id','in',$category_id]]]);
            array_push($child_category_id,$category_id);
            $where=[['category_id','in',$child_category_id],['country_id','=',cache('lang_id')]];
            if(!empty($p['keyword']))
            {
                $where[]=['title','like','%'.$p['keyword'].'%'];
            }
            $params['where']=$where;
            $params['field']='id,title,add_time,category_id,images';
            $params['query']=$p;
            $datalist = ArticleService::datalist($params);      
            $pager = $datalist['pager'];
            $list=$datalist['datalist'];
        }
        View::assign('data_list',$list);
        View::assign('category_id',$category_id);
        View::assign('page',$pager);
        return View::fetch();
    }
    public function case()
    {
        $p=input();
        $category_id=2;
        View::assign('category_id',$category_id);
        $where=[['category_id','=',$category_id],['country_id','=',cache('lang_id')]];
        
        if(!empty($p['keyword']))
        {
            $where[]=['title','like','%'.$p['keyword'].'%'];
        }
        $params['where']=$where;
        $params['field']='id,title,add_time,category_id';
        $params['query']=$p;
        $datalist = ArticleService::datalist($params);      
        $page = $datalist['pager'];
        View::assign('data_list',$datalist['datalist']);
        View::assign('page',$page);
        return View::fetch();
    }
    public function about()
    {
        $p=input();
        $category_id=4;
        View::assign('category_id',$category_id);
        $where=[['category_id','=',$category_id],['country_id','=',cache('lang_id')]];
        $data = ArticleService::info(['where'=>$where]);
        View::assign('data',$data);
        return View::fetch();
    }
    public function project()
    {
        $p=input();
        $category_id=3;
        View::assign('category_id',$category_id);
        $where=[['category_id','=',$category_id],['country_id','=',cache('lang_id')]];
        if(!empty($p['keyword']))
        {
            $where[]=['title','like','%'.$p['keyword'].'%'];
        }
        $params['where']=$where;
        $params['field']='id,title,add_time,category_id';
        $params['query']=$p;
        $datalist = ArticleService::datalist($params);      
        $page = $datalist['pager'];
        View::assign('data_list',$datalist['datalist']);
        View::assign('page',$page);
        return View::fetch();
    }
    public function contact()
    {
        $p=input();
        $category_id=6;
        View::assign('category_id',$category_id);
        $where=[['category_id','=',$category_id],['country_id','=',cache('lang_id')]];
        $data = ArticleService::info(['where'=>$where]);
        View::assign('data',$data);
        return View::fetch();
    }
    public function notice()
    {
        $p=input();
        $category_id=5;
        View::assign('category_id',$category_id);
        $where=[['category_id','=',$category_id],['country_id','=',cache('lang_id')]];
        if(!empty($p['keyword']))
        {
            $where[]=['title','like','%'.$p['keyword'].'%'];
        }
        $params['where']=$where;
        $params['field']='id,title,add_time,category_id';
        $params['query']=$p;
        $datalist = ArticleService::datalist($params);      
        $page = $datalist['pager'];
        View::assign('data_list',$datalist['datalist']);
        View::assign('page',$page);
        return View::fetch();
    }
    public function modify()
    {
        $p=input();
        $data=array();
        if(empty($p['id']))
        {
            $parent_id=empty($p['parent_id'])?0:intval($p['parent_id']);
        }
        else        
        {
            $data = Db::name('article')->where(['id'=>intval($p['id'])])->find();
            if($data['category_id']>0)
            {
                $parent_id=\app\service\CategoryService::info(['where'=>['id'=>$data['category_id']]])['parent_id'];
            }
            $data['images_str']=$data['images'];
            $data['images']=handleImg($data['images']);
        }
        View::assign('parent_id',$parent_id);
        View::assign('category',\app\service\CategoryService::getTreeList(['parent_id'=>$parent_id]));
        View::assign('data',$data);
        return View::fetch();
    }
    public function save()
    {
        $p=input();
        if(empty($p['title']))
        {
        jsonReturn(0,'标题不能为空');
        }
        unset($p['file']);
        //return cookie('admin_token_login');
        $p['user_id']=$this->user['id'];
        $p['add_time']=time();
        $p['country_id']=cache('lang_id');
        $p['category_id']=empty($p['category_id'])?$p['parent_category_id']:$p['category_id'];
        
        if(empty($p['is_top']))
        {
            $p['is_top'] = 0;
        }
        if(empty($p['status']))
        {
            $p['status'] = 0;
        }
        if(empty($p['id']))
        {
            $res = Db::name('article')->insert($p);
        }
        else{
            $id = $p['id'];
            unset($p['id']);            
            $res = Db::name('article')->where(['id'=>$id])->update($p);
        }
        return jsonRes($res);
    }
    public function status()
    {
        $p = input();
        $res = Db::name('article')->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }
    public function remove()
    {
        $p = input();
        $res = Db::name('article')->where(['id'=>intval($p['id'])])->delete();
        return jsonRes($res);
    }
    public function removeall()
    {
        $p = input();
        $res = Db::name('article')->where([['id','in',$p['ids']]])->delete();
        return jsonRes($res);
    }
}